package top.carrotguo.weibo.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import top.carrotguo.weibo.R;
import top.carrotguo.weibo.utils.UIUtils;

public class CommentPhotoAdapter extends BaseAdapter {

    private static final int TYPE_ADD = 1;
    private static final int TYPE_PHOTO = 2;

    private List<Uri> datas;
    private Context mContext;
    private RemoveListener mRemoveListener;
    private AddListener mAddListerer;

    public CommentPhotoAdapter(Context context,List<Uri> datas){
        this.datas = datas;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return datas.size()>0?datas.size()+1 : 0;
    }

    @Override
    public Uri getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PhotoHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_comment_photo,null);
            holder = new PhotoHolder();
            holder.ivPic = convertView.findViewById(R.id.iv_pic);
            holder.root = convertView.findViewById(R.id.root);
            holder.ivRemove = convertView.findViewById(R.id.iv_remove);
            convertView.setTag(holder);
        } else {
            holder = (PhotoHolder) convertView.getTag();
        }

        if (datas.size() == 0) {
            holder.root.setVisibility(View.GONE);
        } else {
            holder.root.setVisibility(View.VISIBLE);
            //根据父控件大小计算图片大小
//            GridView gridView = (GridView) parent;
//            int columnCount = gridView.getNumColumns();
//            int itemWidth = (gridView.getMeasuredWidth()-gridView.getHorizontalSpacing()*(columnCount-1)
//                    - gridView.getPaddingLeft() - gridView.getPaddingRight())/columnCount;
//            RelativeLayout.LayoutParams photoParams = new RelativeLayout.LayoutParams(itemWidth,itemWidth);
//            holder.ivPic.setLayoutParams(photoParams);
            if (getItemViewType(position) == TYPE_PHOTO) {
                //Logger.e("width:"+itemWidth);我也不明白为什么有时候加载不全
                Glide.with(mContext).load(datas.get(position)).into(holder.ivPic);
                holder.ivRemove.setVisibility(View.VISIBLE);
            } else {
                if (position<9) {
                    Glide.with(mContext).load(R.drawable.compose_pic_add).into(holder.ivPic);
                    holder.ivRemove.setVisibility(View.INVISIBLE);
                } else {
                    holder.ivRemove.setVisibility(View.GONE);
                    holder.ivPic.setVisibility(View.GONE);
                }
            }

            //点击删除按钮的事件
            holder.ivRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mRemoveListener != null) {
                        mRemoveListener.onRemoveClickListener(position);
                    }
                }
            });

            //添加图片的点击事件
            holder.ivPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mAddListerer != null && position == getCount()-1) {
                        mAddListerer.onAddClickListener();
                    }
                }
            });

        }
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getCount() - 1) {
            return TYPE_ADD;
        } else {
            return TYPE_PHOTO;
        }
    }

    class PhotoHolder{
        ImageView ivPic;
        ImageView ivRemove;
        RelativeLayout root;
    }

    public void setRemoveListener(RemoveListener removeListener){
        this.mRemoveListener = removeListener;
    }

    public void setAddListener(AddListener addListener){
        this.mAddListerer = addListener;
    }

    public interface RemoveListener{
        void onRemoveClickListener(int position);
    }

    public interface AddListener{
        void onAddClickListener();
    }

}
